package com.zhongxi.guoyuan.view.personalCenter;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.DynamicsAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.center.DynamicsData;
import com.zhongxi.guoyuan.entity.user.center.DynamicsResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.ArrayList;
import java.util.List;

public class DynamicsActivity extends BaseActivity implements View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView>{

    private PullToRefreshListView lvList;
    private DynamicsAdapter adapter;
    private List<DynamicsData> data = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_dynamics);
        initTitle();
        tvTitle.setText("最新动态");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new DynamicsAdapter(context, data);
        lvList.setAdapter(adapter);
        getDynamics();
    }

    private void getDynamics() {
        String str = "&page="+pageNum;
        getServer(Url.ACTIVITY_LIST, null, str, false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            DynamicsResult result = gson.fromJson(response, DynamicsResult.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            if(pageNum==1){
                data.clear();
            }
            if(result.getData().size()>0){
                data.addAll(result.getData());
                adapter.notifyDataSetChanged();
                lvList.onRefreshComplete();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
        Log.i("base", response);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnRefreshListener(this);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, DynamicsDetailActivity.class);
                intent.putExtra("html",data.get(position-1).getDetailspage());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum =1;
        getDynamics();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getDynamics();
    }
}
